package corbaEventService;

import org.omg.CORBA.ORB;
import org.omg.CORBA.Object;
import org.omg.CosNaming.NameComponent;
import org.omg.CosNaming.NamingContextExt;
import org.omg.CosNaming.NamingContextExtHelper;
import org.omg.PortableServer.POA;
import org.omg.PortableServer.POAHelper;

import cosEventChannelAdmin.EventChannel;
import cosEventChannelAdmin.EventChannelHelper;
import cosEventComm.PullSupplier;
import cosEventComm.PullSupplierHelper;
import cosEventComm.PushConsumer;
import cosEventComm.PushConsumerHelper;

public class EventServiceFactory {
	private ORB orb;
	private String arg[] = {"-ORBInitialPort","1050","-ORBInitialHost","localhost"};
	private Object objRef;
	private NamingContextExt ncRef;
	private POA rootpoa;
	
	public EventServiceFactory() throws Exception {
		super();		
		init();
	}
	
	public EventServiceFactory(String host,String port) throws Exception {
		super();
		arg[3] = host;
		arg[1] = port;		
		init();
	}
	
	private void init() throws Exception{
		//Create ORB		
		orb = ORB.init(arg,null);
		
		//Get reference to rootpoa
		rootpoa = POAHelper.narrow(orb.resolve_initial_references("RootPOA"));
	
		//Activate POAManager
		rootpoa.the_POAManager().activate();
		
		//Get the root naming context
		objRef  = orb.resolve_initial_references("NameService");
		ncRef = NamingContextExtHelper.narrow(objRef);
	}
	
	public EventChannel createEventChannel(String eventChannelName) throws Exception{
		//Create servant
		EventChannelImpl channel = new EventChannelImpl();
		
		//Activate Servant and get its refernece
		Object ref = rootpoa.servant_to_reference(channel);
		
		//Bind the Servant
		NameComponent path[] = ncRef.to_name(eventChannelName);
		ncRef.rebind(path, ref);
		
		return EventChannelHelper.narrow(ref);
	}
	
	public EventChannel getEventChannel(String eventChannelName) throws Exception{		
		return EventChannelHelper.narrow(ncRef.resolve_str(eventChannelName));
	}	
	
	public PushConsumer createPushConsumer(String channelName,String pushConsumerName) throws Exception{
		//Look up for channel
		EventChannel channel = EventChannelHelper.narrow(ncRef.resolve_str(channelName));
		
		//Create servant
		PushConsumerImpl supplier = new PushConsumerImpl(channel);
		
		//Activate Servant and get its refernece
		Object ref = rootpoa.servant_to_reference(supplier);
		
		//Bind the Servant
		NameComponent path[] = ncRef.to_name(pushConsumerName);
		ncRef.rebind(path, ref);
		
		return PushConsumerHelper.narrow(ref);
	}
	
	public PullSupplier createPullSupplier(String channelName,String pullSupplierName) throws Exception{
		//Look up for channel
		EventChannel channel = EventChannelHelper.narrow(ncRef.resolve_str(channelName));
		
		//Create servant
		PullSupplierImpl consumer = new PullSupplierImpl(channel);
		
		//Activate Servant and get its refernece
		Object ref = rootpoa.servant_to_reference(consumer);
		
		//Bind the Servant
		NameComponent path[] = ncRef.to_name(pullSupplierName);
		ncRef.rebind(path, ref);
		
		return PullSupplierHelper.narrow(ref);
	}
	

}
